#include <bits/stdc++.h>

using namespace std;
int c[1000][1000];
void slove() {
    int n, m;
    cin >> n >> m;
    vector<char> a(n + 1), b(m + 1);
    for (int  i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= m; i++) {
        cin >> b[i];
    }
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++) {
            if (a[i] == b[j]) c[i][j] = c[i-1][j-1] + 1;
            else c[i][j] = max(c[i-1][j],c[i][j-1]);
        }
    }
    cout << c[n][m];
}
int main() {
    slove();
    return 0;
}
